﻿using System.Diagnostics;
using System.Reflection;

namespace UnityEngine
{
    /// <summary>
    /// 日志模块的扩展类
    /// </summary>
    public static class DebuggerExtension
    {
        /// <summary>
        /// LogTag是调用打印日志的类中自定义的常量字符串，通常情况下LogTag是类名。
        /// 用LogTag可以直观地看出这条日志是哪个类输出的。
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string GetLogTag(object obj)
        {
            FieldInfo field = obj.GetType().GetField("LOG_TAG");
            if (field != null)
            {
                return (string)field.GetValue(obj);
            }
            return obj.GetType().Name;
        }

        [Conditional("EnableLog")]
        public static void Log(this object obj, string message)
        {
            if (Debugger.EnableLog)
            {
                Debugger.Log(GetLogTag(obj), message);
            }
        }

        [Conditional("EnableLog")]
        public static void Log(this object obj, string format, params object[] args)
        {
            if (Debugger.EnableLog)
            {
                string message = string.Format(format, args);
                Debugger.Log(GetLogTag(obj), message);
            }
        }

        [Conditional("EnableLog")]
        public static void LogError(this object obj, string message)
        {
            Debugger.LogError(GetLogTag(obj), message);
        }

        [Conditional("EnableLog")]
        public static void LogError(this object obj, string format, params object[] args)
        {
            string message = string.Format(format, args);
            Debugger.LogError(GetLogTag(obj), message);
        }

        [Conditional("EnableLog")]
        public static void LogWarning(this object obj, string message)
        {
            Debugger.LogWarning(GetLogTag(obj), message);
        }

        [Conditional("EnableLog")]
        public static void LogWarning(this object obj, string format, params object[] args)
        {
            string message = string.Format(format, args);
            Debugger.LogWarning(GetLogTag(obj), message);
        }

    }
}